package JsonInterface;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
import ImplementInterface.ImplementWebInterface;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class WebFacet extends ActionSupport implements ServletRequestAware,SessionAware{
	public HttpServletRequest request;
	public JSONObject jMap;
	public JSONArray jList;
	public ImplementWebInterface Webface;
	
	public HttpServletResponse response;
	public String result;
	public JSONArray ja;
	public String name;
	public String password;
	public Integer userId;
	public Integer privilege;
	public Map<String, Object> session;
	public HttpSession session1;
//	public static boolean isCreateSocket = false;
//	public static DatagramSocket serverSocket;
	public ImplementWebInterface getWebface() {
		return Webface;
	}

	public void setWebface(ImplementWebInterface webface) {
		Webface = webface;
	}

	public JSONObject getjMap() {
		return jMap;
	}

	public void setjMap(JSONObject jMap) {
		this.jMap = jMap;
	}

	public JSONArray getjList() {
		return jList;
	}

	public void setjList(JSONArray jList) {
		this.jList = jList;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
			// TODO Auto-generated method stub
			this.request=arg0;
			this.userId = (Integer)this.request.getSession().getAttribute("userId");
			this.privilege = (Integer)this.request.getSession().getAttribute("privilege");
	}

	
	public void setResult(String result) {
		this.result = result;
	}
	
public String getUserMessage(){
		String userName=request.getParameter("name");
		String password=request.getParameter("password");
		Map map=Webface.getUserMessage(userName,password);
		session1=request.getSession();
		if (map.get("flag") != "fail") {
			session1.setAttribute("userId", map.get("flag"));
			session1.setAttribute("privilege", map.get("privilege"));
		}
		result=JSONObject.fromObject(map).toString();
		return SUCCESS;
}
	
public String userAction(){
	    Map map = Webface.getUserMessage(name,password);
		if(map.get("flag") !="fail"){
	        session1=request.getSession();
	        session1.setAttribute("userId", map.get("flag"));
	        session1.setAttribute("privilege", map.get("privilege"));
			return "success";
		}else{
			return "fail";
		}
}

public String checkLogin() {
	result="false";
	if (userId != null) result="true";
	return SUCCESS;
}

public String getWarningNumber() {
	List list = Webface.getWarningNumber(privilege);
	result = JSONArray.fromObject(list).toString();
	return SUCCESS;
}

public String getWarningData() {
	String startdate = request.getParameter("startdate");
	String stopdate = request.getParameter("stopdate");
	int pageCount = Integer.valueOf(request.getParameter("pagecount"));
	List list = Webface.getWarningData(startdate, stopdate, privilege, pageCount);
	result = JSONArray.fromObject(list).toString();
	return SUCCESS;
}
//public String createSocketUdp() {
//
//	if (!isCreateSocket) {
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				isCreateSocket = true;
////				DatagramSocket serverSocket = null;
//				try {
//					serverSocket = new DatagramSocket(20003);// 20003
//					System.out.println("Socket was created with the port 20003!");
//				} catch (SocketException e1) {
//					e1.printStackTrace();
//				}
//				byte[] recvBuf = new byte[100];
//				DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
////				WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
////				ImplementInterface za = (ImplementInterface) context.getBean("interface");
//				while (true) {
//					try {
//						serverSocket.receive(recvPacket);
//						 System.out.println("FROM:"+recvPacket.getAddress().getHostAddress()+"PORT:"+recvPacket.getPort()+" LENGTH:"+recvPacket.getLength());
//						 System.out.println(new Date().toString());
//						if ((recvPacket.getLength() == 22)&&(((int) recvBuf[12] & 0xff)==0x03)) {
//							int crc = CRC16.getCRC1021(recvBuf, recvPacket.getLength() - 2);
//							if (((crc >> 8 & 0xff) == ((int) recvBuf[22 - 2] & 0xff))
//									&& ((crc & 0xff) == ((int) recvBuf[22 - 1] & 0xff))) {
//								
//								 int d1=((int)recvBuf[0]&0xff)-0x30; 
//						    	 int d2=((int)recvBuf[1]&0xff)-0x30; 
//						    	 int d3=((int)recvBuf[2]&0xff)-0x30; 
//						    	 int d4=((int)recvBuf[3]&0xff)-0x30; 
//						    	 int d5=((int)recvBuf[4]&0xff)-0x30; 
//						    	 int d6=((int)recvBuf[5]&0xff)-0x30; 
//						    	 int d7=((int)recvBuf[6]&0xff)-0x30; 
//						    	 int d8=((int)recvBuf[7]&0xff)-0x30; 
//						    	 int d9=((int)recvBuf[8]&0xff)-0x30; 
//						    	 int d10=((int)recvBuf[9]&0xff)-0x30; 
//						    	 int d11=((int)recvBuf[10]&0xff)-0x30; 
//						    	 int d12=((int)recvBuf[11]&0xff)-0x30; 
//						    	 String address_str=d1+""+d2+""+d3+""+d4+""+d5+""+d6+""+d7+""+d8+""+d9+""+d10+""+d11+""+d12;
//						    	 System.out.println("ZKP51_Aaddress:"+address_str);
//								
//								int current = ((((int) recvBuf[14] & 0xff) << 8) + ((int) recvBuf[15] & 0xff));
//								int voltage = ((((int) recvBuf[16] & 0xff) << 8) + ((int) recvBuf[17] & 0xff));
//								int cost = ((((int) recvBuf[18] & 0xff) << 8) + ((int) recvBuf[19] & 0xff));
//								 System.out.println("current*10:"+current+"A");
//								 System.out.println("voltage*10:"+voltage+"V");
//								 System.out.println("cost:"+cost+"kwh");
//								
//								String status="";
//								if(((int) recvBuf[19] & 0xff)==0x00) {
//									status="分闸";
//								}else if(((int) recvBuf[19] & 0xff)==0x01) {
//									status="合闸";
//								}
//								List list=IAI.GetDevList(address_str);
//								 
//								Thread.sleep(1000);
//								byte[] buf = new byte[] { (byte) 0xff };
//								SocketAddress sk = new InetSocketAddress(recvPacket.getAddress().getHostAddress(),
//										recvPacket.getPort());
//								DatagramPacket packet = new DatagramPacket(buf, buf.length, sk);
//								serverSocket.send(packet);
////								face.addZaixiancewenData(map);
//							}
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}
//		}).start();
//	} else
//		System.out.print(" socketudp is failed!");
//	return SUCCESS;
//}	


}
